﻿using BasicData.API.Read.Application.Commands;
using BasicData.Domain;
using BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BasicData.API.Read.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MangementController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<MangementController> logger;

        public MangementController(IMediator mediator,ILogger<MangementController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 测试
        /// </summary>
        /// <returns>返回测试任务</returns>
        [HttpGet]
        public IActionResult Get()
        {
            try
            {
                logger.LogInformation("测试成功");
                int num = int.Parse("ABC");
                return Ok("完成");
            }
            catch (Exception ex)
            {
                logger.LogError("测试失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 全局测试
        /// </summary>
        /// <returns>返回全局测试任务</returns>
        [HttpGet]
        public IActionResult GetLog()
        {
            try
            {
                logger.LogInformation("全局测试成功");
                int num = 0;
                int num1 = 1;
                int num2 = num1 / num;
                return Ok("完成");
            }
            catch (Exception ex)
            {
                logger.LogError("全局测试失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns>返回登录任务</returns>
        [HttpPost]
        public Task<APIResult<AccountModel>> Login(LoginCommand command)
        {
            try
            {
                logger.LogInformation("登录成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("登录失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns>返回显示任务</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<GoodsModel>>> GetGoods([FromQuery]GetGoodsCommand command)
        {
            try
            {
                logger.LogInformation("显示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("显示失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示产品
        /// </summary>
        /// <returns>返回显示产品任务</returns>
        [HttpGet]
        public Task<APIResult<List<ChanModel>>> GetChan([FromQuery]GetChanCommand command)
        {
            try
            {
                logger.LogInformation("显示产品成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("显示产品失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示仓库
        /// </summary>
        /// <returns>返回仓库产品任务</returns>
        [HttpGet]
        public Task<APIResult<List<CangModel>>> GetCang([FromQuery] GetCangCommand command)
        {
            try
            {
                logger.LogInformation("显示仓库成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("显示仓库失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示库房
        /// </summary>
        /// <returns>返回库房产品任务</returns>
        [HttpGet]
        public Task<APIResult<List<KuModel>>> GetKu([FromQuery] GetKuCommand command)
        {
            try
            {
                logger.LogInformation("显示库房成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("显示库房失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示库房
        /// </summary>
        /// <returns>返回库房产品任务</returns>
        [HttpGet]
        public Task<APIResult<List<HuoModel>>> GetHuo([FromQuery] GetHuoCommand command)
        {
            try
            {
                logger.LogInformation("显示库房成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("显示库房失败", ex.Message);
                throw;
            }
        }
    }
}
